package cn.yunyichina.provider.bone.utils;

import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.framework.dubbo.service.MessageService;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.entity.message.ImageTemplateListVo;
import cn.yunyichina.provider.iface.entity.message.ImageTemplateVo;
import cn.yunyichina.provider.iface.entity.message.TmpTemplateVo;
import cn.yunyichina.utils.convert.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by Joe.zhou on 2017/9/11.
 */
public class BoneMsgUtils {
    private static final Logger logger = LoggerFactory.getLogger(BoneMsgUtils.class);

    private static final String MAIN_TEMPLATECODE = "IMG22010";

    public static String getActionUrl(String templateCode){
        logger.info("[BoneMsgUtils.getActionUrl templateCode-->]" + templateCode);
        String url = "";
        ImageTemplateVo imageTemplateVo = new ImageTemplateVo();
        imageTemplateVo.setTemplateCode(MAIN_TEMPLATECODE);
        Response response = BaseService.callHumpFromInternal("yyt.base.image.template.by.code.get", JsonUtils.toJsonString(imageTemplateVo));
        logger.info("获取骨科链接模板 yyt.base.image.template.by.code.get -->" + JsonUtils.toJsonString(response.getResult()));
        if("0".equals(response.getResultCode())) {
            List<ImageTemplateListVo> images = JsonUtils.parseArray(response.getResult(), ImageTemplateListVo.class);
            ImageTemplateListVo imageTemplate = null;
            for (ImageTemplateListVo image : images) {
                if (templateCode.equals(image.getImageTemplateCode())) {
                    imageTemplate = image;
                    break;
                }
            }

            if(imageTemplate != null){
                url = imageTemplate.getActionUrl();
            }else{
                logger.info("获取骨科链接模板失败 templatCode:" + templateCode);
            }
        }

        return url;
    }

    public static String queryActionUrl(String templateCode){
        String url = "";
        TmpTemplateVo templateVo =  new TmpTemplateVo();
        templateVo.setTemplateCode(templateCode);
        logger.info("获取骨科链接模板   yyt.message.tmp.template.get --> 入参" + JsonUtils.toJsonString(templateVo));
        Response response = MessageService.callHumpFromInternal("yyt.message.tmp.template.get", JsonUtils.toJsonString(templateVo));
        logger.info("获取骨科链接模板 yyt.message.tmp.template.get -->" + JsonUtils.toJsonString(response.getResult()));
        if("0".equals(response.getResultCode())) {
            List<TmpTemplateVo> templateVos = JsonUtils.parseArray(response.getResult(), TmpTemplateVo.class);
            url = templateVos.get(0).getUrl();
        }
        return url;
    }
}
